import glob
import os

import pandas as pd
from PySide2.QtCore import Qt
from PySide2.QtGui import QKeySequence, QClipboard
from PySide2.QtWidgets import QWidget, QLabel, QTableWidget, QVBoxLayout, QTableWidgetItem, QHeaderView, QSizePolicy, \
    QStyledItemDelegate, QComboBox, QTabWidget
from forms.uipy.UI_ledger import Ui_ledger
from tools.File_Select_Tools import File_Select_Tools
from tools.问卷 import 问卷
from server.LedgerServer import LedgerServer
from tools.Area_tools import Area_Tools
from functools import cmp_to_key

class CenterDelegate(QStyledItemDelegate):
    def paint(self, painter, option, index):
        option.displayAlignment = Qt.AlignCenter
        super().paint(painter, option, index)


class LedgerImpl(QWidget, Ui_ledger):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        # 当状态为True时，用户重新加载数据，否则没有重新加载数据，仅仅是查询
        self.status = False
        self.run_status.setHidden(True)

        # 台账业务类
        self.ledger_server = None
        self.show_data_combox = QComboBox()
        self.show_data_combox.hide()
        self.gridLayout.addWidget(self.show_data_combox, 3, 1)

        self.data_dir_path.setDisabled(True)
        # 清除 tab widget 所有tab
        self.tab_show.clear()

        # 绑定区域选择下拉框  切换事件
        self.area_select.activated.connect(self.area_select_handle)

        # 绑定范围选择下拉框  切换事件
        self.range_select.activated.connect(self.range_selected_change)

        # 绑定选择数据文件夹按钮
        self.data_dir_btn.clicked.connect(self.data_dir_btn_handle)

        # 绑定运行按钮函数

        self.ok_run.clicked.connect(self.ok_run_handle)



        self.基本信息_表头 = ["本期成员变动情况", "姓名", "年龄", "与户主关系", "何种医疗保险", "是否在校学生",
                              "过去三个月居住时间", "是否常住成员", "有无共享收入", "是否离退休人员", "何种养老保险",
                              "是否从业过", "就业状况", "主要行业", "主要职业", "工作时间"]
        self.基本信息_表头2 = ["B105建筑面积(保留整数)", "B106住宅外道路路面情况", "B108主要饮用水来源",
                               "B110_2厨房使用情况", "B112厕所使用情况", "B111厕所类型", "B111_2粪便清掏和处理情况",
                               "B112_2是否有洗手设施、肥皂和水", "B113洗澡设施", "B114主要取暖设备状况",
                               "B115主要取暖用能源状况", "B116主要炊用能源状况", "B201家用汽车", "B201_1其中新能源汽车",
                               "B202摩托车", "B203助力车", "B204洗衣机", "B205电冰箱(柜)", "B207彩色电视机", "B208烤箱",
                               "B209空调", "B210热水器", "B213洗碗机", "B214排油烟机", "B216移动电话", "B218计算机",
                               "B221照相机", "B222乐器(500元以上)", "B223健身器材", "B225空气净化器(含新风系统)",
                               "B206微波炉", "B215固定电话", "B226地面清洁电器"]

    def range_selected_change(self):
        """
        范围选择切换
        :return:
        """
        current_text = self.range_select.currentText()

        if current_text == "分点":
            # 显示分点内容
            self.show_txt.setText("点位选择")
            # 住户SID 输入框隐藏
            self.show_data.hide()
            # 点位下拉框显示并初始化数据
            self.show_data_combox.show()
            self.point_combox_init(self.ledger_server.get_area_point(self.area_select.currentData()))
            # self.show_data.setDisabled(False)
            self.show_data_combox.setDisabled(False)
            pass
        elif current_text == "分户":
            # 显示分户内容
            self.show_txt.setText("分户SID")
            # 住户SID 输入框显示
            self.show_data.show()
            # 点位下拉框显示并初始化数据
            self.show_data_combox.hide()
            self.show_data.setDisabled(False)
            pass
        else:
            # 选择全部
            self.show_data.setDisabled(True)
            self.show_data_combox.setDisabled(True)
            pass

    def point_combox_init(self, points: []):
        """
        点位下拉框初始化数据
        :param points:
        :return:
        """
        self.show_data_combox.clear()
        for point in points:
            self.show_data_combox.addItem(str(point), point)
        # self.show_data_combox.addItems(points)

    def data_dir_btn_handle(self):
        """
        点击选择文件处理函数
        :return:
        """
        self.status = True
        try:
            dir_path = File_Select_Tools.select_dir(title="选择数据文件夹")
            self.data_dir_path.setText(dir_path)
        except Exception as e:
            return False
        # 创建台账业务类
        try:
            self.ledger_server = LedgerServer(dir_path)
        except Exception as e:
            self.run_status.setText("程序运行错误,请检查数据格式!!!")
            self.run_status.setHidden(False)
        # 初始化 区域选择下拉框
        self.area_select_init(self.ledger_server.get_area_code())

    def area_select_init(self, data: []):
        """
        区域选择框初始化
        :return:
        """
        self.area_select.clear()
        if (len(data) == 7):
            # 七个区县全齐 添加泸州市
            self.area_select.addItem("泸州市", 510500)
        for code in data:
            self.area_select.addItem(Area_Tools.get_area_name(code), code)

        if len(data) == 1:
            self.area_select.setCurrentIndex(0)
            self.area_select.setDisabled(True)
            self.area_select_handle()
            self.show_data.setDisabled(True)
            self.show_data_combox.setDisabled(True)
        else:
            self.area_select.setCurrentIndex(0)
            self.area_select.setDisabled(False)
            self.area_select_handle()
            self.show_data.setDisabled(True)
            self.show_data_combox.setDisabled(False)
            self.show_data_combox.hide()

    def area_select_handle(self):
        # 区域选择切换处理事件
        # 将范围选择清空
        self.range_select.clear()
        self.range_select.addItem("全部")
        # 泸州市没有分点
        if self.area_select.currentData() != 510500:
            self.range_select.addItem("分点")

        self.range_select.addItem("分户")

        # 初始化区县数据
        self.ledger_server.generate_pivot_tables(self.area_select.currentData())

    def ok_run_handle(self):
        """
        点击运行按钮
        :return:
        """
        # 排除泸州市 不显示 基本信息清空
        # 区县 分点 显示 该点下的所有户基本信息
        # 分户 只显示 该户基本信息

        try:

            self.tab_show.clear()

            # 分户
            if not self.show_data.isHidden() and len(self.show_data.text()) != 0:
                self.tab_show.addTab(self.基本信息(self.show_data.text()),"基本信息")
                self.tab_show.addTab(self.居民收入调查现金账逻辑关系审核表_show(house_id=self.show_data.text()),"收入")
                self.tab_show.addTab(self.消费_show(house_id=self.show_data.text()),"消费支出")

            # 分点
            if not self.show_data_combox.isHidden():
                point = self.show_data_combox.currentData()
                # self.tab_show.addTab(self.基本信息(self.show_data.text()), "基本信息")
                self.tab_show.addTab(self.居民收入调查现金账逻辑关系审核表_show(point=point), "收入")
                self.tab_show.addTab(self.消费_show(point=point), "消费支出")

            # 全部
            if self.range_select.currentText() == "全部":
                self.tab_show.clear()
                self.tab_show.addTab(self.居民收入调查现金账逻辑关系审核表_show(), "收入")
                self.tab_show.addTab(self.消费_show(), "消费支出")

        except Exception as e:
            self.run_status.setText("程序运行错误,请检查数据格式!!!")
            self.run_status.setHidden(False)
            pass
        # if self.area_select.currentData() != 510500:
        #     w = self.基本信息("510502002002101H09901")
        #     self.tab_show.addTab(w, "基本信息")
        #     pass

        # if not self.show_data_combox.isHidden():
        #     self.tab_show.addTab(self.居民收入调查现金账逻辑关系审核表_show())
        # print(self.show_data_combox.isHidden())

        # self.tab_show.addTab(self.居民收入调查现金账逻辑关系审核表_show(), "收入")
        # self.tab_show.addTab(self.消费_show(), "消费")
        # self.tab_show.addTab(QLabel("四收八支"), "四收八支")
    def keyPressEvent(self, event):
        # 重写键盘按下事件
        if event.matches(QKeySequence.StandardKey.Copy):
            # print(self.findChildren(QTableWidget))
            select_items = None

            for table_obj in self.findChildren(QTableWidget):
                select_items = table_obj.selectedIndexes()

            if select_items is None:
                return None

            # print(select_items)
            # temp:QWidget = self.tab_show.currentWidget()
            #
            # print(temp)
            # select_items = self.show_table.selectedIndexes()
            if len(select_items) > 0:
                # 使用cmp_to_key转换, 排序
                select_items = sorted(select_items, key=cmp_to_key(self.__sort_select_items))
                # 行数
                row_num = select_items[0].row()

                min_row = select_items[0].row()
                max_row = select_items[-1].row()
                min_col = select_items[0].column()
                temp_min_col = min_col
                max_col = select_items[-1].column()
                # for item in select_items:
                #     if item.row() == row_num:
                #         str = str + item.data() + "\t"
                #     else:
                #         str = str + "\n" + item.data()+"\t"
                #         row_num = item.row()
                str_arr = []
                while min_row <= max_row:
                    row = []
                    while temp_min_col <= max_col:
                        flag = 0
                        for item in select_items:
                            if item.row() == min_row and item.column() == temp_min_col:
                                row.append(item.data())
                                flag = 1
                                break
                        if flag == 0:
                            row.append("")
                        temp_min_col += 1
                        flag = 0
                    temp_min_col = min_col
                    str_arr.append(row)
                    min_row += 1
                # 添加到剪切板
                # 将二维数组转化为字符串
                # 添加到剪贴板数据
                str = ""
                for row in str_arr:
                    for col in row:
                        str += col + "\t"
                    str += "\n"
                # print(str)
                # 获取剪切板对象
                QClipboard().setText(str)
                # print(min_row,max_row)
                # print(min_col,max_col)
    def 基本信息(self, house_id) -> QWidget:
        widget = QWidget()

        layout = QVBoxLayout()
        data: pd.DataFrame = self.ledger_server.调查户基本信息情况表1(house_id)

        table1 = QTableWidget()
        table1.setRowCount(data.shape[0] + 1)
        table1.setColumnCount(data.shape[1] + 1)

        table1.verticalHeader().hide()
        # 设置表头
        header = QTableWidgetItem("调查户基本信息情况表1")
        header.setTextAlignment(Qt.AlignCenter)

        table1.setItem(0, 0, header)
        # 设置列头
        table1.setItem(1, 0, QTableWidgetItem(""))
        for i in range(len(self.基本信息_表头)):
            table1.setItem(i + 2, 0, QTableWidgetItem(self.基本信息_表头[i]))

        # 设置数据
        for row_index, row in data.iterrows():
            for column_index, value in row.items():
                if pd.isna(value):
                    table1.setItem(row_index + 1, column_index + 1, QTableWidgetItem(str("")))
                    continue
                table1.setItem(row_index + 1, column_index + 1, QTableWidgetItem(str(value)))

        # 合并表头,
        table1.setSpan(0, 0, 1, data.shape[1] + 1)
        table1.horizontalHeader().sectionResizeMode(QHeaderView.ResizeToContents)
        # table.resize(500,600)
        table1.setMinimumHeight(700)
        # 表格居中 铺满屏幕
        table1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table1.horizontalHeader().setStretchLastSection(True)
        table1.resizeColumnsToContents()
        table1.setItemDelegate(CenterDelegate())

        layout.addWidget(table1)

        # 添加表 调查户基本信息情况表2
        data2 = list(self.ledger_server.调查户基本信息情况表2(house_id).iloc[0])
        table2: pd.Series = QTableWidget()
        table2.setRowCount(9)
        table2.setColumnCount(10)

        table2.verticalHeader().hide()

        # 设置表头与初始值
        table2.setItem(0, 0, QTableWidgetItem("调查户基本信息情况表2"))
        table2.setItem(1, 0, QTableWidgetItem("住房情况"))
        table2.setItem(1, 4, QTableWidgetItem("耐用消费品"))
        # 初始化值

        step = len(data2) // 6
        colum_index = 1
        total_index = 0
        # for index, value in enumerate(data2):
        for s in range(step):
            for row_index in range(6):
                # 设置问题
                table2.setItem(row_index + 2, colum_index - 1, QTableWidgetItem(self.基本信息_表头2[total_index]))
                if pd.isna(data2[total_index]):
                    table2.setItem(row_index + 2, colum_index, QTableWidgetItem(""))
                else:
                    # 设置值
                    table2.setItem(row_index + 2, colum_index, QTableWidgetItem(str(data2[total_index])))
                total_index += 1
            colum_index += 2

        ## 单独设置特殊三项
        table2.setItem(8, 4, QTableWidgetItem(self.基本信息_表头2[len(data2) - 3]))
        table2.setItem(8, 5, QTableWidgetItem(str(data2[len(data2) - 3])))
        table2.setItem(8, 6, QTableWidgetItem(self.基本信息_表头2[len(data2) - 2]))
        table2.setItem(8, 7, QTableWidgetItem(str(data2[len(data2) - 2])))
        table2.setItem(8, 8, QTableWidgetItem(self.基本信息_表头2[len(data2) - 1]))
        table2.setItem(8, 9, QTableWidgetItem(str(data2[len(data2) - 1])))
        table2.setMinimumHeight(400)

        # 表格居中 铺满屏幕
        table2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table2.horizontalHeader().setStretchLastSection(True)
        table2.resizeColumnsToContents()
        table2.setItemDelegate(CenterDelegate())

        # 合并单元格
        table2.setSpan(0, 0, 1, 10)
        table2.setSpan(1, 0, 1, 4)
        table2.setSpan(1, 4, 1, 10)

        layout.addWidget(table2)

        widget.setLayout(layout)

        return widget
    def __sort_select_items(self, a, b):
        """
        自定义排序选择数据 1 0 -1
        :param a: 当前项
        :param b: 后一项
        :return:
        """
        if a.row() < b.row():
            return -1
        elif a.row() == b.row():
            # 同行，排序列
            if a.column() < b.column():
                return -1
            elif a.column() == b.column():
                return 0
            else:
                return 1
        else:
            return 1
    def 居民收入调查现金账逻辑关系审核表_show(self, point=None, house_id=None):
        data:pd.DataFrame = self.ledger_server.居民收入调查现金账逻辑关系审核表(point, house_id)
        table = QTableWidget()
        table.setRowCount(61)
        table.setColumnCount(15)

        self.居民收入调查现金账逻辑关系审核表_show_layout(table)
        column_index = 2
        for row_index, row in data.iterrows():
            for j, value in row.items():
                if pd.isna(value):
                    table.setItem(row_index, column_index, QTableWidgetItem(""))
                else:
                    table.setItem(row_index,column_index,QTableWidgetItem(str(value)))
                column_index += 1
                if column_index == 15:
                    column_index = 2
        # 表格居中 铺满屏幕
        table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table.horizontalHeader().setStretchLastSection(True)
        table.resizeColumnsToContents()
        table.setItemDelegate(CenterDelegate())
        return table

    def 居民收入调查现金账逻辑关系审核表_show_layout(self, table: QTableWidget):
        # 设置表头
        table.setHorizontalHeaderLabels(["","项目","12月","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","全年"])

        # table.setItem(, 0, QTableWidgetItem("项目"))
        table.setItem(0, 0, QTableWidgetItem("工资性收入"))
        table.setSpan(0,0,1,2)
        table.setItem(1, 1, QTableWidgetItem("按月发放的工资"))
        table.setItem(2, 1, QTableWidgetItem("补发工资"))
        table.setItem(3, 1, QTableWidgetItem("不按月发放的工资"))
        table.setItem(4, 1, QTableWidgetItem("其他工资"))
        table.setItem(5, 1, QTableWidgetItem("交纳所得税"))
        table.setItem(6, 1, QTableWidgetItem("个人缴纳的养老保险"))
        table.setItem(7, 1, QTableWidgetItem("个人缴纳的医疗保险"))
        table.setItem(8, 1, QTableWidgetItem("个人缴纳的失业保险"))
        table.setItem(9, 1, QTableWidgetItem("个人缴纳的住房公积金"))
        table.setItem(10, 1, QTableWidgetItem("个人缴纳的职业年金"))
        table.setItem(11, 1, QTableWidgetItem("其他社会保障支出"))
        table.setSpan(1, 0, 11, 1)

        table.setItem(12, 0, QTableWidgetItem("实物账"))
        table.setSpan(12, 0, 3, 1)

        table.setItem(12, 1, QTableWidgetItem("单位的实物和服务"))
        table.setItem(13, 1, QTableWidgetItem("政府的实物和服务"))
        table.setItem(14, 1, QTableWidgetItem("从社会实物和服务"))
        table.setItem(15, 0, QTableWidgetItem("经营性收入"))
        table.setSpan(15, 0, 1, 2)
        table.setItem(16, 0, QTableWidgetItem("经营性净收入=经营收入-经营成本"))
        table.setSpan(16, 0, 1, 2)
        table.setItem(17, 1, QTableWidgetItem("一产收入"))
        table.setItem(18, 1, QTableWidgetItem("一产成本"))
        table.setItem(19, 1, QTableWidgetItem("二三产收入"))
        table.setItem(20, 1, QTableWidgetItem("二三产成本"))
        table.setSpan(17, 0, 4, 1)
        table.setItem(21, 0, QTableWidgetItem("财产性收入"))
        table.setSpan(21, 0, 1, 2)
        table.setItem(22, 0, QTableWidgetItem("财产净收入=财产性收入-财产性支出"))
        table.setSpan(22, 0, 1, 2)

        table.setItem(23, 0, QTableWidgetItem("财产支出"))
        table.setSpan(23, 0, 2, 1)
        table.setItem(23, 1, QTableWidgetItem("其他生活贷款利息支出"))
        table.setItem(24, 1, QTableWidgetItem("住房贷款利息支出"))

        table.setItem(25, 0, QTableWidgetItem("住房利息支出和住房本金支出是配套账页"))
        table.setSpan(25, 0, 2, 1)
        table.setItem(25, 1, QTableWidgetItem("归还住房贷款本金"))
        table.setItem(26, 1, QTableWidgetItem("借贷性支出"))
        table.setItem(27, 0, QTableWidgetItem("转移性收入"))
        table.setSpan(27, 0, 1, 2)
        table.setItem(28, 0, QTableWidgetItem("转移净收入=转移性收入-转移性支出"))
        table.setSpan(28, 0, 1, 2)
        table.setItem(29, 0, QTableWidgetItem("(一)养老金或离退休金"))
        table.setSpan(29, 0, 1, 2)
        table.setItem(30, 1, QTableWidgetItem("离退休人员养老金或离退休金"))
        table.setItem(31, 1, QTableWidgetItem("城乡居民基本养老保险"))
        table.setItem(32, 1, QTableWidgetItem("其他养老金"))
        table.setSpan(30, 0, 3, 1)
        table.setItem(33, 0, QTableWidgetItem("(二)社会救济和补助"))
        table.setSpan(33, 0, 1, 2)
        table.setSpan(34, 0, 6, 1)
        table.setItem(34, 1, QTableWidgetItem("最低生活保障费"))
        table.setItem(35, 1, QTableWidgetItem("五保户救助金"))
        table.setItem(36, 1, QTableWidgetItem("定期发放的抚恤金"))
        table.setItem(37, 1, QTableWidgetItem("医疗救助专项补贴"))
        table.setItem(38, 1, QTableWidgetItem("残疾人两项补贴"))
        table.setItem(39, 1, QTableWidgetItem("其他社会救济收入"))
        table.setItem(40, 0, QTableWidgetItem("(三)政策性生产补贴"))
        table.setSpan(40, 0, 1, 2)
        table.setSpan(41, 0, 3, 1)
        table.setItem(41, 1, QTableWidgetItem("农业支持保护补贴"))
        table.setItem(42, 1, QTableWidgetItem("畜牧业补贴"))
        table.setItem(43, 1, QTableWidgetItem("其他农业生产补贴"))
        table.setItem(44, 0, QTableWidgetItem("(五)政策性生活补贴"))
        table.setSpan(44, 0, 1, 2)
        table.setItem(45, 0, QTableWidgetItem("(五)报销医疗费"))
        table.setSpan(45, 0, 1, 2)
        table.setItem(46, 1, QTableWidgetItem("住院医疗总费用（一般有报销）"))
        table.setItem(47, 0, QTableWidgetItem("(六)外出从业人员寄回带回收入"))
        table.setSpan(47, 0, 1, 2)
        table.setSpan(48, 0, 2, 1)
        table.setItem(48, 1, QTableWidgetItem("城镇寄带回收入"))
        table.setItem(49, 1, QTableWidgetItem("农村寄带回收入"))
        table.setItem(50, 0, QTableWidgetItem("（七）赡养收入"))
        table.setSpan(50, 0, 1, 2)
        table.setItem(51, 0, QTableWidgetItem("(八)其他经常转移收入"))
        table.setSpan(51, 0, 1, 2)
        table.setItem(52, 0, QTableWidgetItem("不算入可支配收入的项目"))
        table.setSpan(52, 0, 4, 1)
        table.setItem(52, 1, QTableWidgetItem("非收入所得"))
        table.setItem(53, 1, QTableWidgetItem("    博彩所得"))
        table.setItem(54, 1, QTableWidgetItem("    调查补贴"))
        table.setItem(55, 1, QTableWidgetItem("借贷性所得"))
        table.setItem(56, 0, QTableWidgetItem("转移性支出"))
        table.setSpan(56, 0, 5, 1)
        table.setItem(56, 1, QTableWidgetItem("1.个人所得税"))
        table.setItem(57, 1, QTableWidgetItem("2.社会保障支出"))
        table.setItem(58, 1, QTableWidgetItem("3.外来从业人员寄给家人的支出"))
        table.setItem(59, 1, QTableWidgetItem("4.赡养支出"))
        table.setItem(60, 1, QTableWidgetItem("5.其他转移性支出"))

    def 消费_show(self, point=None, house_id=None):
        tab = QTabWidget()
        tab.addTab(self.食品烟酒(point, house_id),"食品烟酒")
        tab.addTab(self.生活消费(point, house_id),"生活消费")
        tab.addTab(self.文娱_医疗_美发(point, house_id),"文娱、医疗、美发等消费")
        tab.addTab(self.农业支出_农村点(point, house_id),"农业支出")
        tab.addTab(self.畜牧业支出_农村点(point, house_id),"畜牧业支出")
        tab.addTab(self.自产自用消费_农村点(point, house_id),"自产自用消费")
        tab.addTab(self.自产自用畜牧业饲料_农村点(point, house_id),"自产自用畜牧业饲料")
        tab.addTab(self.农作物产量_农村点(point, house_id),"农作物产量")

        return tab

    def 食品烟酒(self,point=None, house_id=None):
        data = self.ledger_server.住户调查食品烟酒消费逻辑关系审核表(point,house_id)
        table = QTableWidget()
        table.setRowCount(22)
        table.setColumnCount(14)

        # print(data)

        # 设置表格
        table.setItem(0,0,QTableWidgetItem("住户调查食品烟酒消费逻辑关系审核表"))
        table.setItem(1,11,QTableWidgetItem("单位：元"))

        table.setItem(2,0,QTableWidgetItem("项目"))
        table.setItem(2,1,QTableWidgetItem("12月"))
        table.setItem(2,2,QTableWidgetItem("1月"))
        table.setItem(2,3,QTableWidgetItem("2月"))
        table.setItem(2,4,QTableWidgetItem("3月"))
        table.setItem(2,5,QTableWidgetItem("4月"))
        table.setItem(2,6,QTableWidgetItem("5月"))
        table.setItem(2,7,QTableWidgetItem("6月"))
        table.setItem(2,8,QTableWidgetItem("7月"))
        table.setItem(2,9,QTableWidgetItem("8月"))
        table.setItem(2,10,QTableWidgetItem("9月"))
        table.setItem(2,11,QTableWidgetItem("10月"))
        table.setItem(2,12,QTableWidgetItem("11月"))
        table.setItem(2,13,QTableWidgetItem("全年"))

        table.setItem(3, 0, QTableWidgetItem("大米"))
        table.setItem(4, 0, QTableWidgetItem("食用油"))
        table.setItem(5, 0, QTableWidgetItem("面粉制品"))
        table.setItem(6, 0, QTableWidgetItem("薯类"))
        table.setItem(7, 0, QTableWidgetItem("豆类"))
        table.setItem(8, 0, QTableWidgetItem("蔬菜"))
        table.setItem(9, 0, QTableWidgetItem("肉类"))
        table.setItem(10, 0, QTableWidgetItem("禽类"))
        table.setItem(11, 0, QTableWidgetItem("水产品"))
        table.setItem(12, 0, QTableWidgetItem("蛋类"))
        table.setItem(13, 0, QTableWidgetItem("奶类"))
        table.setItem(14, 0, QTableWidgetItem("干鲜瓜果类"))
        table.setItem(15, 0, QTableWidgetItem("糖果糕点类"))
        table.setItem(16, 0, QTableWidgetItem("饮料"))
        table.setItem(17, 0, QTableWidgetItem("调味品"))
        table.setItem(18, 0, QTableWidgetItem("烟酒"))
        table.setItem(19, 0, QTableWidgetItem("在外饮食服务"))
        table.setItem(20, 0, QTableWidgetItem("婚丧礼金收入"))
        table.setItem(21, 0, QTableWidgetItem("婚丧宴请支出"))
        # 合并单元格
        table.setSpan(0,0,1,14)
        table.setSpan(1,0,1,11)
        table.setSpan(1,11,1,3)
        # 填充数据
        column_index = 1

        for row_index, row in data.iterrows():
            for j, value in row.items():
                if pd.isna(value):
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(""))
                else:
                    table.setItem(row_index+3,column_index,QTableWidgetItem(str(value)))
                column_index += 1
                if column_index == 14:
                    column_index = 1

        # 表格居中 铺满屏幕
        table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table.horizontalHeader().setStretchLastSection(True)
        table.resizeColumnsToContents()
        table.setItemDelegate(CenterDelegate())
        return table

    def 生活消费(self,point=None, house_id=None):
        data = self.ledger_server.住户调查生活消费逻辑关系审核表(point, house_id)
        table = QTableWidget()
        table.setRowCount(21)
        table.setColumnCount(14)

        # 设置表格
        table.setItem(0, 0, QTableWidgetItem("住户调查生活消费逻辑关系审核表"))
        table.setItem(1, 11, QTableWidgetItem("单位：元"))

        table.setItem(2, 0, QTableWidgetItem("项目"))
        table.setItem(2, 1, QTableWidgetItem("12月"))
        table.setItem(2, 2, QTableWidgetItem("1月"))
        table.setItem(2, 3, QTableWidgetItem("2月"))
        table.setItem(2, 4, QTableWidgetItem("3月"))
        table.setItem(2, 5, QTableWidgetItem("4月"))
        table.setItem(2, 6, QTableWidgetItem("5月"))
        table.setItem(2, 7, QTableWidgetItem("6月"))
        table.setItem(2, 8, QTableWidgetItem("7月"))
        table.setItem(2, 9, QTableWidgetItem("8月"))
        table.setItem(2, 10, QTableWidgetItem("9月"))
        table.setItem(2, 11, QTableWidgetItem("10月"))
        table.setItem(2, 12, QTableWidgetItem("11月"))
        table.setItem(2, 13, QTableWidgetItem("全年"))

        table.setItem(3, 0, QTableWidgetItem("衣类"))
        table.setItem(4, 0, QTableWidgetItem("鞋类"))
        table.setItem(5, 0, QTableWidgetItem("租房.房屋维修.物管费等"))
        table.setItem(6, 0, QTableWidgetItem("水费"))
        table.setItem(7, 0, QTableWidgetItem("电费"))
        table.setItem(8, 0, QTableWidgetItem("生活燃料"))
        table.setItem(9, 0, QTableWidgetItem("家用纺织品"))
        table.setItem(10, 0, QTableWidgetItem("洗涤卫生用品"))
        table.setItem(11, 0, QTableWidgetItem("个人护理用品"))
        table.setItem(12, 0, QTableWidgetItem("家庭服务"))
        table.setItem(13, 0, QTableWidgetItem("交通费"))
        table.setItem(14, 0, QTableWidgetItem("交通工具燃料"))
        table.setItem(15, 0, QTableWidgetItem("交通工具使用及维修(含税）"))
        table.setItem(16, 0, QTableWidgetItem("机动车辆保险"))
        table.setItem(17, 0, QTableWidgetItem("电话费"))
        table.setItem(18, 0, QTableWidgetItem("上网费"))
        table.setItem(19, 0, QTableWidgetItem("一揽子通信服务"))
        table.setItem(20, 0, QTableWidgetItem("邮费+其他通信服务费"))


        # 合并单元格
        table.setSpan(0, 0, 1, 14)
        table.setSpan(1, 0, 1, 11)
        table.setSpan(1, 11, 1, 3)
        # 填充数据
        column_index = 1

        for row_index, row in data.iterrows():
            for j, value in row.items():
                if pd.isna(value):
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(""))
                else:
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(str(value)))
                column_index += 1
                if column_index == 14:
                    column_index = 1

        # 表格居中 铺满屏幕
        table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table.horizontalHeader().setStretchLastSection(True)
        table.resizeColumnsToContents()
        table.setItemDelegate(CenterDelegate())

        return table

    def 文娱_医疗_美发(self,point=None, house_id=None):
        data = self.ledger_server.住户调查文娱_医疗_美发等消费逻辑关系审核表(point, house_id)
        table = QTableWidget()
        table.setRowCount(17)
        table.setColumnCount(14)

        # 设置表格
        table.setItem(0, 0, QTableWidgetItem("住户调查文娱_医疗_美发等消费逻辑关系审核表"))
        table.setItem(1, 11, QTableWidgetItem("单位：元"))

        table.setItem(2, 0, QTableWidgetItem("项目"))
        table.setItem(2, 1, QTableWidgetItem("12月"))
        table.setItem(2, 2, QTableWidgetItem("1月"))
        table.setItem(2, 3, QTableWidgetItem("2月"))
        table.setItem(2, 4, QTableWidgetItem("3月"))
        table.setItem(2, 5, QTableWidgetItem("4月"))
        table.setItem(2, 6, QTableWidgetItem("5月"))
        table.setItem(2, 7, QTableWidgetItem("6月"))
        table.setItem(2, 8, QTableWidgetItem("7月"))
        table.setItem(2, 9, QTableWidgetItem("8月"))
        table.setItem(2, 10, QTableWidgetItem("9月"))
        table.setItem(2, 11, QTableWidgetItem("10月"))
        table.setItem(2, 12, QTableWidgetItem("11月"))
        table.setItem(2, 13, QTableWidgetItem("全年"))

        table.setItem(3, 0, QTableWidgetItem("教育"))
        table.setItem(4, 0, QTableWidgetItem("文化娱乐"))
        table.setItem(5, 0, QTableWidgetItem("  其中团体旅游"))
        table.setItem(6, 0, QTableWidgetItem("  其中文化娱乐服务"))
        table.setItem(7, 0, QTableWidgetItem("  其中有线电视费"))
        table.setItem(8, 0, QTableWidgetItem("药品"))
        table.setItem(9, 0, QTableWidgetItem("滋补保健品"))
        table.setItem(10, 0, QTableWidgetItem("门诊医疗"))
        table.setItem(11, 0, QTableWidgetItem("住院医疗"))
        table.setItem(12, 0, QTableWidgetItem("其他用品"))
        table.setItem(13, 0, QTableWidgetItem("美容美发洗浴"))
        table.setItem(14, 0, QTableWidgetItem("旅馆住宿费"))
        table.setItem(15, 0, QTableWidgetItem("博彩支出"))
        table.setItem(16, 0, QTableWidgetItem("婚丧嫁娶礼金支出"))




        # 合并单元格
        table.setSpan(0, 0, 1, 14)
        table.setSpan(1, 0, 1, 11)
        table.setSpan(1, 11, 1, 3)
        # 填充数据
        column_index = 1

        for row_index, row in data.iterrows():
            for j, value in row.items():
                if pd.isna(value):
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(""))
                else:
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(str(value)))
                column_index += 1
                if column_index == 14:
                    column_index = 1

        # 表格居中 铺满屏幕
        table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table.horizontalHeader().setStretchLastSection(True)
        table.resizeColumnsToContents()
        table.setItemDelegate(CenterDelegate())

        return table

    def 农业支出_农村点(self,point=None, house_id=None):
        data = self.ledger_server.住户调查农业支出逻辑关系审核表_农村点(point, house_id)
        table = QTableWidget()
        table.setRowCount(14)
        table.setColumnCount(14)

        # 设置表格
        table.setItem(0, 0, QTableWidgetItem("住户调查农业支出逻辑关系审核表(农村点)"))
        table.setItem(1, 11, QTableWidgetItem("单位：元"))

        table.setItem(2, 0, QTableWidgetItem("项目"))
        table.setItem(2, 1, QTableWidgetItem("12月"))
        table.setItem(2, 2, QTableWidgetItem("1月"))
        table.setItem(2, 3, QTableWidgetItem("2月"))
        table.setItem(2, 4, QTableWidgetItem("3月"))
        table.setItem(2, 5, QTableWidgetItem("4月"))
        table.setItem(2, 6, QTableWidgetItem("5月"))
        table.setItem(2, 7, QTableWidgetItem("6月"))
        table.setItem(2, 8, QTableWidgetItem("7月"))
        table.setItem(2, 9, QTableWidgetItem("8月"))
        table.setItem(2, 10, QTableWidgetItem("9月"))
        table.setItem(2, 11, QTableWidgetItem("10月"))
        table.setItem(2, 12, QTableWidgetItem("11月"))
        table.setItem(2, 13, QTableWidgetItem("全年"))

        table.setItem(3, 0, QTableWidgetItem("稻谷种子"))
        table.setItem(4, 0, QTableWidgetItem("玉米种子"))
        table.setItem(5, 0, QTableWidgetItem("其他种子及种苗"))
        table.setItem(6, 0, QTableWidgetItem("自产自用种子(公斤)"))
        table.setItem(7, 0, QTableWidgetItem("农药"))
        table.setItem(8, 0, QTableWidgetItem("肥料"))
        table.setItem(9, 0, QTableWidgetItem("薄膜"))
        table.setItem(10, 0, QTableWidgetItem("生产经营雇工"))
        table.setItem(11, 0, QTableWidgetItem("机收机耕费"))
        table.setItem(12, 0, QTableWidgetItem("可能是错误编码"))
        table.setItem(13, 0, QTableWidgetItem("购建农业生产性固定资产"))


        # 合并单元格
        table.setSpan(0, 0, 1, 14)
        table.setSpan(1, 0, 1, 11)
        table.setSpan(1, 11, 1, 3)
        # 填充数据
        column_index = 1

        for row_index, row in data.iterrows():
            for j, value in row.items():
                if pd.isna(value):
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(""))
                else:
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(str(value)))
                column_index += 1
                if column_index == 14:
                    column_index = 1

        # 表格居中 铺满屏幕
        table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table.horizontalHeader().setStretchLastSection(True)
        table.resizeColumnsToContents()
        table.setItemDelegate(CenterDelegate())

        return table


    def 畜牧业支出_农村点(self,point=None, house_id=None):
        data = self.ledger_server.住户调查畜牧业支出逻辑关系审核表_农村点(point, house_id)
        table = QTableWidget()
        table.setRowCount(10)
        table.setColumnCount(14)

        # 设置表格
        table.setItem(0, 0, QTableWidgetItem("住户调查畜牧业支出逻辑关系审核表(农村点)"))
        table.setItem(1, 11, QTableWidgetItem("单位：元"))

        table.setItem(2, 0, QTableWidgetItem("项目"))
        table.setItem(2, 1, QTableWidgetItem("12月"))
        table.setItem(2, 2, QTableWidgetItem("1月"))
        table.setItem(2, 3, QTableWidgetItem("2月"))
        table.setItem(2, 4, QTableWidgetItem("3月"))
        table.setItem(2, 5, QTableWidgetItem("4月"))
        table.setItem(2, 6, QTableWidgetItem("5月"))
        table.setItem(2, 7, QTableWidgetItem("6月"))
        table.setItem(2, 8, QTableWidgetItem("7月"))
        table.setItem(2, 9, QTableWidgetItem("8月"))
        table.setItem(2, 10, QTableWidgetItem("9月"))
        table.setItem(2, 11, QTableWidgetItem("10月"))
        table.setItem(2, 12, QTableWidgetItem("11月"))
        table.setItem(2, 13, QTableWidgetItem("全年"))

        table.setItem(3, 0, QTableWidgetItem("幼畜"))
        table.setItem(4, 0, QTableWidgetItem("饲料"))
        table.setItem(5, 0, QTableWidgetItem("兽药"))
        table.setItem(6, 0, QTableWidgetItem("畜禽防疫费"))
        table.setItem(7, 0, QTableWidgetItem("生产经营雇工"))
        table.setItem(8, 0, QTableWidgetItem("牧业其他费用"))
        table.setItem(9, 0, QTableWidgetItem("渔业支出"))


        # 合并单元格
        table.setSpan(0, 0, 1, 14)
        table.setSpan(1, 0, 1, 11)
        table.setSpan(1, 11, 1, 3)
        # 填充数据
        column_index = 1

        for row_index, row in data.iterrows():
            for j, value in row.items():
                if pd.isna(value):
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(""))
                else:
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(str(value)))
                column_index += 1
                if column_index == 14:
                    column_index = 1

        # 表格居中 铺满屏幕
        table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table.horizontalHeader().setStretchLastSection(True)
        table.resizeColumnsToContents()
        table.setItemDelegate(CenterDelegate())

        return table

    def 自产自用消费_农村点(self,point=None, house_id=None):
        data = self.ledger_server.住户调查自产自用消费逻辑关系审核表_农村点(point, house_id)
        table = QTableWidget()
        table.setRowCount(21)
        table.setColumnCount(14)

        # 设置表格
        table.setItem(0, 0, QTableWidgetItem("住户调查自产自用消费逻辑关系审核表(农村点)"))
        table.setItem(1, 11, QTableWidgetItem("单位：公斤"))

        table.setItem(2, 0, QTableWidgetItem("项目"))
        table.setItem(2, 1, QTableWidgetItem("12月"))
        table.setItem(2, 2, QTableWidgetItem("1月"))
        table.setItem(2, 3, QTableWidgetItem("2月"))
        table.setItem(2, 4, QTableWidgetItem("3月"))
        table.setItem(2, 5, QTableWidgetItem("4月"))
        table.setItem(2, 6, QTableWidgetItem("5月"))
        table.setItem(2, 7, QTableWidgetItem("6月"))
        table.setItem(2, 8, QTableWidgetItem("7月"))
        table.setItem(2, 9, QTableWidgetItem("8月"))
        table.setItem(2, 10, QTableWidgetItem("9月"))
        table.setItem(2, 11, QTableWidgetItem("10月"))
        table.setItem(2, 12, QTableWidgetItem("11月"))
        table.setItem(2, 13, QTableWidgetItem("全年"))

        table.setItem(3, 0, QTableWidgetItem("稻谷"))
        table.setItem(4, 0, QTableWidgetItem("玉米"))
        table.setItem(5, 0, QTableWidgetItem("红薯"))
        table.setItem(6, 0, QTableWidgetItem("马铃薯"))
        table.setItem(7, 0, QTableWidgetItem("豆类"))
        table.setItem(8, 0, QTableWidgetItem("油菜籽"))
        table.setItem(9, 0, QTableWidgetItem("甘蔗"))
        table.setItem(10, 0, QTableWidgetItem("动物油"))
        table.setItem(11, 0, QTableWidgetItem("露地蔬菜"))
        table.setItem(12, 0, QTableWidgetItem("自产自用酒"))
        table.setItem(13, 0, QTableWidgetItem("水果"))
        table.setItem(14, 0, QTableWidgetItem("家畜"))
        table.setItem(15, 0, QTableWidgetItem("家禽"))
        table.setItem(16, 0, QTableWidgetItem("蛋类"))
        table.setItem(17, 0, QTableWidgetItem("养殖鱼.虾"))
        table.setItem(18, 0, QTableWidgetItem("捕捞鱼.虾"))
        table.setItem(19, 0, QTableWidgetItem("蜂蜜"))
        table.setItem(20, 0, QTableWidgetItem("柴"))



        # 合并单元格
        table.setSpan(0, 0, 1, 14)
        table.setSpan(1, 0, 1, 11)
        table.setSpan(1, 11, 1, 3)
        # 填充数据
        column_index = 1

        for row_index, row in data.iterrows():
            for j, value in row.items():
                if pd.isna(value):
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(""))
                else:
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(str(value)))
                column_index += 1
                if column_index == 14:
                    column_index = 1

        # 表格居中 铺满屏幕
        table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table.horizontalHeader().setStretchLastSection(True)
        table.resizeColumnsToContents()
        table.setItemDelegate(CenterDelegate())

        return table

    def 自产自用畜牧业饲料_农村点(self,point=None, house_id=None):
        data = self.ledger_server.住户调查自产自用畜牧业饲料逻辑关系审核表_农村点(point, house_id)
        table = QTableWidget()
        table.setRowCount(9)
        table.setColumnCount(14)

        # 设置表格
        table.setItem(0, 0, QTableWidgetItem("住户调查自产自用畜牧业饲料逻辑关系审核表(农村点)"))
        table.setItem(1, 11, QTableWidgetItem("单位：公斤/元"))

        table.setItem(2, 0, QTableWidgetItem("项目"))
        table.setItem(2, 1, QTableWidgetItem("12月"))
        table.setItem(2, 2, QTableWidgetItem("1月"))
        table.setItem(2, 3, QTableWidgetItem("2月"))
        table.setItem(2, 4, QTableWidgetItem("3月"))
        table.setItem(2, 5, QTableWidgetItem("4月"))
        table.setItem(2, 6, QTableWidgetItem("5月"))
        table.setItem(2, 7, QTableWidgetItem("6月"))
        table.setItem(2, 8, QTableWidgetItem("7月"))
        table.setItem(2, 9, QTableWidgetItem("8月"))
        table.setItem(2, 10, QTableWidgetItem("9月"))
        table.setItem(2, 11, QTableWidgetItem("10月"))
        table.setItem(2, 12, QTableWidgetItem("11月"))
        table.setItem(2, 13, QTableWidgetItem("全年"))

        table.setItem(3, 0, QTableWidgetItem("稻谷"))
        table.setItem(4, 0, QTableWidgetItem("玉米"))
        table.setItem(5, 0, QTableWidgetItem("红薯/其他粮食.公斤/元"))
        table.setItem(6, 0, QTableWidgetItem(""))
        table.setItem(7, 0, QTableWidgetItem("青饲料重量"))
        table.setItem(8, 0, QTableWidgetItem("饲料牧草"))

        # 合并单元格
        table.setSpan(0, 0, 1, 14)
        table.setSpan(1, 0, 1, 11)
        table.setSpan(1, 11, 1, 3)
        table.setSpan(5,0,2,1)
        # 填充数据
        column_index = 1

        for row_index, row in data.iterrows():
            for j, value in row.items():
                if pd.isna(value):
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(""))
                else:
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(str(value)))
                column_index += 1
                if column_index == 14:
                    column_index = 1

        # 表格居中 铺满屏幕
        table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table.horizontalHeader().setStretchLastSection(True)
        table.resizeColumnsToContents()
        table.setItemDelegate(CenterDelegate())

        return table

    def 农作物产量_农村点(self,point=None, house_id=None):
        data = self.ledger_server.住户调查农作物产量逻辑关系审核表_农村点(point, house_id)
        table = QTableWidget()
        table.setRowCount(21)
        table.setColumnCount(14)

        # 设置表格
        table.setItem(0, 0, QTableWidgetItem("住户调查农作物产量逻辑关系审核表(农村点)"))
        table.setItem(1, 11, QTableWidgetItem("单位：亩/公斤"))

        table.setItem(2, 0, QTableWidgetItem("项目"))
        table.setItem(2, 1, QTableWidgetItem("12月"))
        table.setItem(2, 2, QTableWidgetItem("1月"))
        table.setItem(2, 3, QTableWidgetItem("2月"))
        table.setItem(2, 4, QTableWidgetItem("3月"))
        table.setItem(2, 5, QTableWidgetItem("4月"))
        table.setItem(2, 6, QTableWidgetItem("5月"))
        table.setItem(2, 7, QTableWidgetItem("6月"))
        table.setItem(2, 8, QTableWidgetItem("7月"))
        table.setItem(2, 9, QTableWidgetItem("8月"))
        table.setItem(2, 10, QTableWidgetItem("9月"))
        table.setItem(2, 11, QTableWidgetItem("10月"))
        table.setItem(2, 12, QTableWidgetItem("11月"))
        table.setItem(2, 13, QTableWidgetItem("全年"))

        table.setItem(3, 0, QTableWidgetItem("稻谷    （亩/公斤）"))
        table.setItem(5, 0, QTableWidgetItem("玉米    （亩/公斤）"))
        table.setItem(7, 0, QTableWidgetItem("高粱    （亩/公斤）"))
        table.setItem(9, 0, QTableWidgetItem("红薯    （亩/公斤）"))
        table.setItem(11, 0, QTableWidgetItem("马铃薯  （亩/公斤）"))
        table.setItem(13, 0, QTableWidgetItem("大豆    （亩/公斤）"))
        table.setItem(15, 0, QTableWidgetItem("其他豆类（亩/公斤）"))
        table.setItem(17, 0, QTableWidgetItem("花生    （亩/公斤）"))
        table.setItem(19, 0, QTableWidgetItem("油菜籽  （亩/公斤）"))
        # 合并
        table.setSpan(3, 0, 2, 1)
        table.setSpan(5, 0, 2, 1)
        table.setSpan(7, 0, 2, 1)
        table.setSpan(9, 0, 2, 1)
        table.setSpan(11, 0, 2, 1)
        table.setSpan(13, 0, 2, 1)
        table.setSpan(15, 0, 2, 1)
        table.setSpan(17, 0, 2, 1)
        table.setSpan(19, 0, 2, 1)


        # 合并单元格
        table.setSpan(0, 0, 1, 14)
        table.setSpan(1, 0, 1, 11)
        table.setSpan(1, 11, 1, 3)
        table.setSpan(5,0,2,1)
        # 填充数据
        column_index = 1

        for row_index, row in data.iterrows():
            for j, value in row.items():
                if pd.isna(value):
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(""))
                else:
                    table.setItem(row_index + 3, column_index, QTableWidgetItem(str(value)))
                column_index += 1
                if column_index == 14:
                    column_index = 1

        # 表格居中 铺满屏幕
        table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        table.horizontalHeader().setStretchLastSection(True)
        table.resizeColumnsToContents()
        table.setItemDelegate(CenterDelegate())

        return table



























































